package com.ecnu.sei.app.resources;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import com.ecnu.sei.app.util.Utils;
import com.ecnu.sei.app.util.Utils.DayHour;

public class Time implements Comparable<Time> {

  private DayHour dayHour = null;
  private DateTime dateTime = null;
  private LocalDate date = null;
  
  public Time() {}
  
  public Time(Time time) {
    this.dateTime = new DateTime(time.getDateTime()); 
    this.date = dateTime.toLocalDate();
    this.dayHour = time.getDayHour();
  }
  
  public Time(long timeInMillis) {
    this.dateTime = new DateTime(timeInMillis);
    this.date = dateTime.toLocalDate();
    this.dayHour = Utils.getDayHourFromStartTime(timeInMillis);
  }
  
  public Time(DateTime dateTime, DayHour dayHour) {
    this.dateTime = dateTime;
    this.date = this.dateTime.toLocalDate();
    this.dayHour = dayHour;
  }

  public void addDays(int days) {
    this.dateTime = this.dateTime.plusDays(days);
    this.date = this.dateTime.toLocalDate();
  }
  public LocalDate getDate() {
    return this.date;
  }
  
  public DateTime getDateTime() {
    return this.dateTime;
  }
  
  public DayHour getDayHour() {
    return this.dayHour;
  }
  
  public void setDayHour(DayHour dayHour) {
    this.dayHour = dayHour;
  }
  
  public void setDateTime(DateTime dateTime) {
    this.dateTime = dateTime;
    this.date = dateTime.toLocalDate();  // date has to be consistent with dateTime
                                         // so we don't expose it 
  }
  
  public boolean isSameHalfDay(Time other) {
    if (date.equals(other.getDate())) {
      if (Utils.isSameHalfDay(dayHour, other.getDayHour())) {
        return true;
      }
    }
    
    return false;
  }
  
  public boolean isContHalfDay(Time other) {
    if (date.equals(other.getDate())) {
      if (Utils.isContHalfDay(dayHour, other.getDayHour())) {
        return true;
      }
    }
    
    return false;
  }
  
  @Override
  public int compareTo(Time other) {
    LocalDate otherDate = other.getDate();
    if (date.compareTo(otherDate) < 0) {
      return -1;
    } else if (date.compareTo(otherDate) > 0) {
      return 1;
    } else {
      return this.dayHour.compareTo(other.getDayHour());
    }
  }

  @Override
  public String toString() {
    return "Time [date=" + date + ", dayHour=" + dayHour + "]";
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((date == null) ? 0 : date.hashCode());
    result = prime * result + ((dayHour == null) ? 0 : dayHour.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Time other = (Time) obj;
    if (date == null) {
      if (other.date != null)
        return false;
    } else if (!date.equals(other.date))
      return false;
    if (dayHour != other.dayHour)
      return false;
    return true;
  }
  
  

}
